layui.define(['table', 'form','table'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form,table = layui.table;

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/bxscn/device/portable_battery/type/list.data'
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
      {field: 'id', title: '类型ID'}
      ,{field: 'name', title: '类型名称', minWidth: 100}
      ,{field: 'thumbnailImg', title: '类型图片', templet: function (record){
              if(record.thumbnailImg) {
                  return '<img src="'+record.thumbnailImg+'"  style="max-height:100%;margin:auto;display:block;"/>';
              }else{
                  return '<img src="'+layui.setter.webRoot + '/layuiadmin/style/res/default01.png" style="max-height:100%;margin:auto;display:block;"/>';
              }
          }}
      ,{field: 'batteryPower', title: '电池功率(W)'}
      ,{field: 'createDate', title: '创建时间', sort: true}
      ,{title: '操作', width: 200, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
    ]]
    ,page: true
    ,limit: 30
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });
  
  //监听搜索
  form.on('submit(LAY-search)', function(data){
    var field = data.field;
    //执行重载
    table.reload('LAY-table', {
      where: field
      ,page: {curr: 1}
    });
  });
  
  //事件
  var active = {
      add: function(){
          layer.open({
              type: 2
              ,title: '新增便携式储能电池类型'
              ,content: layui.setter.webRoot + '/bxscn/device/portable_battery/type/add.html'
              ,maxmin: true
              ,area: ['80%', '500px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/bxscn/device/portable_battery/type/add.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  table.reload('LAY-table');
                                  layer.close(index);
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
     ,update:function(obj){
          layer.open({
              type: 2
              ,title: '修改便携式储能电池类型'
              ,content: layui.setter.webRoot + '/bxscn/device/portable_battery/type/update.html?id='+obj.data.id
              ,maxmin: true
              ,area: ['80%', '500px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/bxscn/device/portable_battery/type/update.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  table.reload('LAY-table');
                                  layer.close(index);
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,setCost:function(obj){
          layer.open({
              type: 2
              ,title: '电池租金设置'
              ,content: layui.setter.webRoot + '/bxscn/device/portable_battery/type/day_cost.html?id='+obj.data.id
              ,maxmin: true
              ,area: ['80%', '80%']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/bxscn/device/portable_battery/type/day_cost/save.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  table.reload('LAY-table');
                                  layer.close(index);
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
    ,del:function(obj){
    	layer.confirm('确定要删除该便携式储能电池类型吗？', function(index){
	    	$.ajax({
	      	  type: "post",
	          url: layui.setter.webRoot + '/bxscn/device/portable_battery/type/del.do',
	          data:{id:obj.data.id},
	  		  dataType:"json",
	  		  success:function(res){
	  			if(res.code == 200){
	  				obj.del();
	  			}else{
	  				layer.msg(res.msg,{icon: 5});
	  			}
	  		  }
	        });
	    	layer.close(index);
	    });
    }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('portable_energy_battery_type_list', {})
});